package com.kingyea.gradle.jsch;


import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class JschSessionFactory {
	private SessionConfiguration configuration;
	
	
	public JschSessionFactory(SessionConfiguration configuration) {
		this.configuration = configuration;
	}
	
	
	public JschSession openSession() throws JSchException {
		JSch jsch = new JSch();
		String username = configuration.getUsername();
		String password = configuration.getPassword();
		String host = configuration.getHost();
		int port = configuration.getPort();
		Session session = jsch.getSession(username, host, port);
		if(password!=null) {
			session.setPassword(password);
		}
		
		session.setConfig(configuration.getConfig());
		session.setTimeout(configuration.getTimeout());
		session.connect();
		return new JschSession(session);
	}
	
	public SessionConfiguration getConfiguration() {
		return this.configuration;
	}
	
}
